package com.zhangx.mq.demo.rabbit.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName TopicRabbitConfig
 * @Description 主题交换机
 * 这个是必须的，建立交换器-路由键-队列绑定关系，如果不创建是发送不了的
 * 比较好的参考文献
 * https://blog.csdn.net/qq_35387940/article/details/100514134
 *
 * *  (星号) 用来表示一个单词 (必须出现的)
 * #  (井号) 用来表示任意数量（零个或多个）单词
 * @Author zhangx
 * @Date 2021/2/20 11:14
 **/
@Slf4j
@Configuration
public class TopicRabbitConfig {

    public final static String QUEUE_MAN = "topic.man";
    public final static String QUEUE_WOMEN = "topic.woman";

    public TopicRabbitConfig(){
        log.info("TopicRabbitConfig");
    }

    @Bean
    public Queue manQueue(){
        return new Queue(QUEUE_MAN);
    }

    @Bean
    public Queue womanQueue(){
        return new Queue(QUEUE_WOMEN);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }

    //将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
    //这样只要是消息携带的路由键是topic.man,才会分发到该队列
//    man队列只能受到man的信息
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(manQueue()).to(exchange()).with(QUEUE_MAN);
    }


    //将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
    // 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
//    所以woman队列也会受到man 的信息+woman
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(womanQueue()).to(exchange()).with("topic.#");
    }

}
